﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicsMarket.Service.IService;
using ElectronicsMarket.Service.Models;

namespace ElectronicsMarket.Service.ServiceImpl
{
    public class ProductServiceImpl : IProductService
    {
        private ElectronicsMarketDBClassesDataContext eMEntity;

        public ProductServiceImpl()
        {
            eMEntity = new ElectronicsMarketDBClassesDataContext();
            
        }

        public IQueryable<PRODUCT> getProductList()
        {
            IQueryable<PRODUCT> productList = eMEntity.PRODUCTs;
            return productList;
        }

        public PRODUCT getProductById(long productId)
        {
            PRODUCT product = eMEntity.PRODUCTs.Single(p => p.PRODUCT_ID == productId);
            return product;
        }

        public List<PRODUCT> getProductByName(string productName)
        {
            var productList = getProductList();
            productList = productList.Where(p => p.PRODUCT_NAME.Contains(productName));
            return productList.ToList();
        }

        public List<PRODUCT> getNewProduct()
        {
            IQueryable<PRODUCT> productList = eMEntity.PRODUCTs;
            List<PRODUCT> newProductList = new List<PRODUCT>();
            foreach (PRODUCT i in productList)
                if (dayDistance(i.PRODUCT_MANUFACTURE_DATE.Value) <= 7)
                    newProductList.Add(i);
            return newProductList;
        }

        public List<PRODUCT> getTopProduct()
        {
            List<PRODUCT> topProductList = eMEntity.PRODUCTs.OrderByDescending(p => p.PRODUCT_MANUFACTURE_DATE).Select(p => p).ToList();
            return topProductList;
        }

        private int dayDistance(DateTime day) 
        {
            try
            {
                TimeSpan diff = DateTime.Now.Subtract(day);
                int days = diff.Days;
                return days;
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
        public void insertProduct( PRODUCT product)
        {
            try
            {
                eMEntity.PRODUCTs.InsertOnSubmit(product);
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }
        }
        public void deleteProduct(PRODUCT product)
        {
            var duct = (from p in eMEntity.PRODUCTs where p.PRODUCT_ID == product.PRODUCT_ID select p).Single();
            eMEntity.PRODUCTs.DeleteOnSubmit(duct);
            try
            {
                eMEntity.SubmitChanges();
            }
            catch
            {
                throw new NotImplementedException();
            }

        }
        public void updateProduct(PRODUCT product)
        {
            PRODUCT prod = eMEntity.PRODUCTs.Single(p => p.PRODUCT_ID == product.PRODUCT_ID);
            prod.PRODUCT_CATEGORY_ID = product.PRODUCT_CATEGORY_ID;
            prod.PRODUCT_DESCRIPTION = product.PRODUCT_DESCRIPTION;
            prod.PRODUCT_NAME = product.PRODUCT_NAME;
            prod.PRODUCT_PRICE = product.PRODUCT_PRICE;
            prod.PRODUCT_UNIT = product.PRODUCT_UNIT;
            prod.PRODUCT_WARRANTY = product.PRODUCT_WARRANTY;
            prod.PRODUCT_MANUFACTURE_ID = product.PRODUCT_MANUFACTURE_ID;
            prod.PRODUCT_MANUFACTURE_DATE = product.PRODUCT_MANUFACTURE_DATE;
            prod.PRODUCT_IMAGE = product.PRODUCT_IMAGE;

            eMEntity.SubmitChanges();

        }
        public long getLastProduciId()
        {
            if (eMEntity.PRODUCTs.ToList() == null)
                return 0;
            else
            {
                return eMEntity.PRODUCTs.OrderByDescending(o => o.PRODUCT_ID).Select(o => o).First().PRODUCT_ID;
            }
        }
      
    }   
}